package com.android.until;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Until {
	static public String Change(HttpResponse httpResponce) {
		String result = null;
		try {

			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(httpResponce.getEntity().getContent()));

			StringBuffer stringBuffer = new StringBuffer("");
			// 获取本系统的行分割线
			String NL = System.getProperty("line.separator");
			// 把http响应的输入流数据按行读取到StringBuffer中
			String line = "";
			while ((line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line + NL);
			}
			result = stringBuffer.toString();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	public static String Get_(String url) {
		String result = null;
		HttpGet get = new HttpGet(url);
		try {
			HttpResponse responce = new DefaultHttpClient().execute(get);
			result = Until.Change(responce);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	public static String Get(String url) {
		String result = null;
		HttpGet get = new HttpGet("http://ssdut.dlut.edu.cn" + url);
		try {
			HttpResponse responce = new DefaultHttpClient().execute(get);
			result = Until.Change(responce);

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static String DeleteHtml(String url) {
		Document doc = Jsoup.parse(Get(url));
		Elements title = doc.select("[class=title]");
		title.text();
		Elements element = doc.select("[class=content]");
		Elements img = element.select("img");
		for (Element e : img) {
			String url1 = e.select("img").attr("src");
			e.select("img").attr("src", "http://ssdut.dlut.edu.cn" + url1);
		}
		return element.toString();

	}

}
